home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / circuits / pcb-1.000 / pcb-1 / pcb-1.3 / parse_l.l < prev    next >
Text File  |  1995-03-01  |  7KB  |  264 lines

  1. %{
  2. /*
  3.  *                            COPYRIGHT
  4.  *
  5.  *  PCB, interactive printed circuit board design
  6.  *  Copyright (C) 1994,1995 Thomas Nau
  7.  *
  8.  *  This program is free software; you can redistribute it and/or modify
  9.  *  it under the terms of the GNU General Public License as published by
  10.  *  the Free Software Foundation; either version 2 of the License, or
  11.  *  (at your option) any later version.
  12.  *
  13.  *  This program is distributed in the hope that it will be useful,
  14.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.  *  GNU General Public License for more details.
  17.  *
  18.  *  You should have received a copy of the GNU General Public License
  19.  *  along with this program; if not, write to the Free Software
  20.  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  *
  22.  *  Contact addresses for paper mail and Email:
  23.  *  Thomas Nau, Schlehenweg 15, 88471 Baustetten, Germany
  24.  *  Thomas.Nau@rz.uni-ulm.de
  25.  *
  26.  */
  27.  
  28. static    char    *rcsid = "$Header: /sda4/users/nau/src/pcb/RCS/parse_l.l,v 2.1 1994/09/28 14:26:59 nau Exp nau $";
  29.  
  30. /* lexical definitions to parse ASCII input of PCB and Element description
  31.  */
  32.  
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. #include <ctype.h>
  36.  
  37. #if defined(_POSIX_SOURCE) || defined(_HPUX_SOURCE)
  38. #include <unistd.h>
  39. #endif
  40.  
  41. #include "global.h"
  42. #include "crosshair.h"
  43. #include "data.h"
  44. #include "error.h"
  45. #include "mymem.h"
  46. #include "misc.h"
  47. #include "parse_l.h"
  48. #include "parse_y.h"
  49.  
  50. #include <X11/cursorfont.h>
  51.  
  52. /* ---------------------------------------------------------------------------
  53.  * some shared parser identifiers
  54.  */
  55. #ifdef FLEX_SCANNER
  56. int                yylineno;        /* linenumber */
  57. #endif
  58.  
  59. char            *yyfilename;    /* in this file */
  60. PCBTypePtr        yyPCB;            /* used by parser */
  61. DataTypePtr        yyData;
  62. ElementTypePtr    yyElement;
  63. FontTypePtr        yyFont;
  64.  
  65. /* ---------------------------------------------------------------------------
  66.  * some local prototypes
  67.  */
  68. static    int        Parse(char *, char *, char *);
  69. static    char    *CreateParseCommandLine(char *, char *, char *);
  70.  
  71. %}
  72.  
  73. HEX            0x[0-9a-fA-F]+
  74. DECIMAL        [0-9]+
  75. STRINGCHAR    ([^"\n\r\\]|\\\\|\\\")
  76.  
  77. %%
  78.  
  79. PCB            { return(T_PCB); }
  80. Grid        { return(T_GRID); }
  81. Cursor        { return(T_CURSOR); }
  82. Flags        { return(T_FLAGS); }
  83. Layer        { return(T_LAYER); }
  84. Pin            { return(T_PIN); }
  85. Via            { return(T_VIA); }
  86. Line        { return(T_LINE); }
  87. Rectangle    { return(T_RECTANGLE); }
  88. Text        { return(T_TEXT); }
  89. ElementLine    { return(T_ELEMENTLINE); }
  90. ElementArc    { return(T_ELEMENTARC); }
  91. Element        { return(T_ELEMENT); }
  92. SymbolLine    { return(T_SYMBOLLINE); }
  93. Symbol        { return(T_SYMBOL); }
  94. Mark        { return(T_MARK); }
  95. Groups        { return(T_GROUPS); }
  96. Polygon        { return(T_POLYGON); }
  97.  
  98. \'.\'                {
  99.                         yylval.number = (unsigned) *(yytext+1);
  100.                         return(CHAR_CONST);
  101.                     }
  102. {DECIMAL}|{HEX}        {
  103.                         sscanf((char *) yytext, "%i", &yylval.number);
  104.                         return(NUMBER);
  105.                     }
  106. \"{STRINGCHAR}*\"    {
  107.                         char    *p1, *p2;
  108.  
  109.                             /* return NULL on empty string */
  110.                         if (yyleng == 2)
  111.                         {
  112.                             yylval.string = NULL;
  113.                             return(STRING);
  114.                         }
  115.  
  116.                             /* allocate memory and copy string;
  117.                              * stringlength is counted and copied without
  118.                              * leading and trailing '"'
  119.                              */
  120.                         yyleng -= 2;
  121.                         yylval.string = MyCalloc(yyleng+1, sizeof(char), "LEX");
  122.                         p1 = (char *) (yytext +1);
  123.                         p2 = yylval.string;
  124.                         while(yyleng--)
  125.                         {
  126.                                 /* check for special character */
  127.                             if (*p1 == '\\')
  128.                             {
  129.                                 yyleng--;
  130.                                 p1++;
  131.  
  132.                             }
  133.                             *p2++ = *p1++;
  134.                         }
  135.                         *p2 = '\0';
  136.                         return(STRING);
  137.                     }
  138. #.*                    {}
  139. [ \t]+                {}
  140. [\n]                {
  141. #ifdef FLEX_SCANNER
  142.                         yylineno++;
  143. #endif
  144.                     }
  145. [\r]                {}
  146. .                    { return(*yytext); }
  147.  
  148. %%
  149.  
  150. /* ---------------------------------------------------------------------------
  151.  * creates the commandline for popen()
  152.  * %f is replaced by the filename 
  153.  * %p by the searchpath
  154.  */
  155. static char *CreateParseCommandLine(char *Template, char *Filename, char *Path)
  156. {
  157.     static    DynamicStringType    command;
  158.             char                *p;
  159.  
  160.     DSClearString(&command);
  161.     for (p = Template; *p; p++)
  162.     {
  163.             /* copy character if not special or add string to command */
  164.         if (!(*p == '%' && (*(p+1) == 'f' || *(p+1) == 'p')))
  165.             DSAddCharacter(&command, *p);
  166.         else
  167.         {
  168.             DSAddString(&command, *(p+1) == 'p' ? Path : Filename);
  169.  
  170.                 /* skip the character */
  171.             p++;
  172.         }
  173.     }
  174.     DSAddCharacter(&command, '\0');
  175.     return(command.Data);
  176. }
  177.  
  178. /* ---------------------------------------------------------------------------
  179.  * sets up the preprocessor command
  180.  */
  181. static int Parse(char *Filename, char *Executable, char *Path)
  182. {
  183.     char    *command;
  184.     int        returncode;
  185.     Cursor    oldCursor;
  186.  
  187. #ifdef FLEX_SCANNER
  188.     static    Boolean    firsttime = True;
  189. #endif
  190.  
  191.         /* create commandline from template */
  192.     command = CreateParseCommandLine(Executable, Filename, Path);
  193.  
  194.         /* open pipe to stdout of command */
  195.     if ((yyin = popen(command, "r")) == NULL)
  196.     {
  197.         PopenErrorMessage(command);
  198.         return(1);
  199.     }
  200.  
  201. #ifdef FLEX_SCANNER
  202.         /* reset parser if not called the first time */
  203.     if (!firsttime)
  204.         yyrestart(yyin);
  205.     firsttime = False;
  206. #endif
  207.  
  208.         /* init linenumber and filename for yyerror() */
  209.     yylineno = 1;
  210.     yyfilename = Filename;
  211.  
  212.         /* We need to save the data temorarily because lex-yacc are able
  213.          * to break the application if the input file has an illegal format.
  214.          * It's not necessary if the system supports the call of functions
  215.          * on termination.
  216.          */
  217.     oldCursor = SetOutputXCursor(XC_watch);
  218.  
  219. #if !defined(HAS_ATEXIT) && !defined(HAS_ON_EXIT)
  220.     SaveTMPData();
  221.     returncode = yyparse();
  222.     RemoveTMPData();
  223. #else
  224.     returncode = yyparse();
  225. #endif
  226.  
  227.     SetOutputXCursor(oldCursor);
  228.     return(pclose(yyin) ? 1 : returncode);
  229. }
  230.  
  231. /* ---------------------------------------------------------------------------
  232.  * initializes LEX and calls parser for a single element
  233.  */
  234. int ParseElement(ElementTypePtr Ptr, char *Filename)
  235. {
  236.     yyPCB = NULL;
  237.     yyFont = &PCB->Font;
  238.     yyElement = Ptr;
  239.     return(Parse(Filename, Settings.ElementCommand, Settings.ElementPath));
  240. }
  241.  
  242. /* ---------------------------------------------------------------------------
  243.  * initializes LEX and calls parser for a complete board
  244.  */
  245. int ParsePCB(PCBTypePtr Ptr, char *Filename)
  246. {
  247.     yyPCB = Ptr;
  248.     yyFont = NULL;
  249.     yyElement = NULL;
  250.     return(Parse(Filename, Settings.FileCommand, Settings.FilePath));
  251. }
  252.  
  253. /* ---------------------------------------------------------------------------
  254.  * initializes LEX and calls parser for a font
  255.  */
  256. int ParseFont(FontTypePtr Ptr, char *Filename)
  257. {
  258.     yyPCB = NULL;
  259.     yyFont = Ptr;
  260.     yyElement = NULL;
  261.     return(Parse(Filename, Settings.FontCommand, Settings.FontPath));
  262. }
  263.  
  264.